home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: Sorting pointers to objects with qsort
- Message-ID: <marnoldDM8Mw7.A1y@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4f18c0$12do@bigblue.oit.unc.edu>
- Date: Sun, 4 Feb 1996 06:19:18 GMT
- Sender: marnold@netcom4.netcom.com
-
- mebust@email.unc.edu (Scott Mebust) writes:
-
- >I'd appreciate any help on the following problem: sorting pointers to
- >objects (based on object attributes) using qsort.
-
- >If replying, please send an e-mail as well. Thanks.
-
- >Scott
- >mebust@email.unc.edu
-
-
- >// QUESTION: Why does the following code return the wrong results?
-
- >// I can't seem to figure this one out. The program below attempts to
- >// sort an array of pointers to String objects using qsort. It compiles
- >// and may *seem* to return the correct results but does not actually
- >// sort the pointers based on any valid data.
-
- >// I believe the problem has to do with "qsort" and "const" objects. It
- >// appears as though qsort is creating new, const String(s) but the
- >// str_ member is not copying correctly.
-
- No, this is a guess and it is way off. Your problem is much more basic.
- See below.
-
- >// This is probably some *elementary* problem but I just can't *see* what
- >// is going wrong.
-
- You are right.
-
- >int StrCmpFn (const void* VP1, const void* VP2)
- >{
- > return strcmp(((const String*)VP1)->GetValue(),
- > ((const String*)VP2)->GetValue());
- >}
-
- You're mistake is here. StrCmpFn is passed pointers to the array
- elements that qsort is sorting. In your case, the array elements
- themselves are pointers! So, StrCmpFn is being passed pointers to
- pointers to String objects, NOT pointers to String objects, which
- is how you are treating them. Change StrCmpFn to take into account
- the *two* levels of indirection and things should work much better.
-
- int StrCmpFn (const void* VP1, const void* VP2)
- {
- const String* ps1 = *(const String**)VP1;
- const String* ps2 = *(const String**)VP2;
-
- return strcmp(ps1->GetValue(), ps2->GetValue());
- }
-
- >void main (void)
- >{
- > String A("Apple"), B("Baseball"), C("Chevrolet");
- > String* SPA[3] = {&C, &B, &A};
-
- > cout << *SPA[0] << endl
- > << *SPA[1] << endl
- > << *SPA[2] << endl << endl;
-
- > qsort(SPA,3,sizeof(SPA[0]),StrCmpFn);
-
- > cout << *SPA[0] << endl
- > << *SPA[1] << endl
- > << *SPA[2] << endl << endl;
- >}
-
-
- Don't feel bad. This is a classic mistake made when learning how to
- use qsort().
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-
-
-
-
-